var request = require('request');
var $ = require("../../include/$");
var tablename = "store_map";
var geotable_id = 123228;
var ak = "yyvKdot9D7eZZvKsYliWchrg";
module.exports = {
	post: function(req, res) {
		var map_db = req.body;
		map_db.create_time = (new Date()).valueOf(); // 创建时间
		map_db.update_time = map_db.create_time; //更新时间
		map_db.description = map_db.description.trim(); //去除描述格式
		map_db.tags = "jewel";
		var form_db = map_db; // 添加到百度云的表单
		form_db.geotable_id = geotable_id;
		form_db.ak = ak;
		form_db.coord_type = 3;
		map_db.store_imgs = map_db.store_imgs.toString();
		request.post({
			url: "http://api.map.baidu.com/geodata/v3/poi/create",
			form: map_db,
			encoding: "utf8"
		}, function(error, response, body) {
			if (response.statusCode == 200) {
				console.log("response_time", (new Date()).toLocaleString());
				map_db.geotable_id = JSON.parse(body).id;
				console.log(map_db);
				if (map_db.geotable_id) {
					req.models[tablename].create(map_db, function(err, result) {
						if (err) {
							res.send(500, err);
						} else {
							// result.store_imgs = result.store_imgs.split(",");
							res.send(result);
						};
						console.log("response_end!");
					});
				} else {
					console.log("no geotable_id!!!");
					console.log("error:", error);
					console.log("body:", body);
					res.send(500, error);
					console.log("response_end!");
				};
			} else {
				console.log("failed", response.statusCode);
				res.send(500, "error", error);
			};
		});
	},
	get: function(req, res) {
		var finner = req.models[tablename].find({}, ["create_time", "Z"]);
		finner.run(function(err, result) {
			if (err) {
				res.send(500, err);
				return;
			};
			if (!result.length) {
				res.send(404, "no found!!!");
				return;
			};
			res.send(result);
		});
	},
	put: function(req, res) {
		req.models[tablename].get(req.body.geotable_id, function(err, result) {
			if (err) {
				res.send(500, err);
				return;
			};
			if (!result) {
				res.send(404, "no found!!!");
				return;
			};
			// 数据库存储的表单
			var map_db = req.body;
			map_db.update_time = (new Date()).valueOf();
			map_db.store_imgs = map_db.store_imgs.toString();
			var key;
			for (key in map_db) {
				result[key] = map_db[key];
			};
			result.save(function(save_err, save_result) {
				if (save_err) {
					res.send(500, save_err);
					return;
				};
				// 百度lbs存储的表单
				var form = map_db;
				form.id = map_db.geotable_id;
				form.geotable_id = geotable_id;
				form.ak = ak;
				form.coord_type = 3;
				request.post({
					url: "http://api.map.baidu.com/geodata/v3/poi/update",
					form: form,
					encoding: "utf8"
				}, function(error, response, body) {
					if (response.statusCode == 200) {
						res.send(save_result);
					} else {
						console.log("failed", response.statusCode);
						res.send(500, "error", error);
					};
				});
			});
		});
	},
	delete: function(req, res) {
		req.models[tablename].get(req.body.geotable_id, function(err, result) {
			if (err) {
				res.send(500, err);
				return;
			};
			if (!result) {
				res.send(404, "no found!!!");
				return;
			};
			result.remove(function(err) {
				if (err) {
					res.send(500, err);
					return;
				};
				var form = {
					geotable_id: geotable_id,
					id: req.body.geotable_id,
					ak: ak
				};
				request.post({
					url: "http://api.map.baidu.com/geodata/v3/poi/delete",
					form: form,
					encoding: "utf8"
				}, function(error, response, body) {
					if (response.statusCode == 200) {
						res.send("ok");
					} else {
						console.log("failed", response.statusCode);
						res.send(500, "error", error);
					};
				});
			});
		});
	}
};